package infoplaces;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Server {
    
    private static final String URL_XML_LL     = "http://cbk0.google.com/cbk?output=xml&ll=";
    private static final String URL_XML_PANOID = "http://cbk0.google.com/cbk?output=xml&panoid=";
    
    private static String getContentOfURL( String path ){
        try {
            
            URL           url      = new URL(path);
            URLConnection url_conn = url.openConnection();
            
            InputStreamReader in_reader   = new InputStreamReader(url_conn.getInputStream());
            BufferedReader    buff_reader = new BufferedReader   (in_reader);
            
            String input = buff_reader.readLine();
            
            buff_reader.close();
            in_reader  .close();
            
            return input;
            
        } catch (MalformedURLException ex) {
            return "";
        } catch (IOException e){
            return "";
        }
    }
    
    public static String getPanoIdOfXML( String lat, String lng ){
        return XML.get_panoID(getContentOfURL( URL_XML_LL + lat + "," + lng) );
    }
    
    public static ArrayList<String> getVizinhosOfXML( String panoid ){
        return XML.getVizinhos(Server.getContentOfURL( URL_XML_PANOID + panoid ) );
    }
    
    public static String getXMLOfURL( String panoid ){
        return getContentOfURL( URL_XML_PANOID + panoid );
    }
    
    public static BufferedImage getImageOfURL( String path ){
        try {
            
            URL           url        = new URL(path);
            BufferedImage buff_image = ImageIO.read( url );

            return buff_image;
            
        } catch (MalformedURLException ex) {
            return null;
        } catch (IOException e){
            return null;
        }
    }
    
}
